from django.shortcuts import render, loader, redirect
from .models import User
from hashlib import md5
from django.http import HttpResponse
from PIL import Image,ImageDraw,ImageFont
import random
import os
from io import BytesIO
# Create your views here.
def login(request):     #定义注册
    context={'title':'登录'}
    return render(request,'/it_fj/login.html/',context)
def login_handler(request): #定义注册
    username = request.POST.get('username')
    password = request.POST.get('password')
    isChecked=request.POST.get('is_checked','')
    if password=='':
        context = {'title':'登录','error':'密码不能为空'}
        return render(request,'/it_fj/login.html/',context)
    u=User()
    if u.findName(username, password):
        request.session['user'] = username
        if isChecked == 'on':
            context = {'title': '个人中心', 's': 1}
            content = loader.render_to_string('info.html', context, request)
            hp = HttpResponse(content)
            hp.set_cookie('user', username)
            return hp
        return redirect('/info/')
    context = {'title': '登录', 'error': '用户名或密码错误，登陆失败'}
    return render(request, 'it_fj/login.html/', context)
    ver = request.POST.get('ver')                       #验证码
    ver_number = request.session.get('code')            #取session里面的值
    if ver == ver_number:                               #判断
        return render(request,'info.html')              #跳转到个人中心页面 验证码输入正确后
    else:
        return render(request,'/itfj/login.html/')
def getcode(request):
    code = request.session.get['code']
    return HttpResponse(code)
def verifyCode(request):                                #验证码生成
    bgcolor = (random.randrange(10,100),random.randrange(10,100),255)
    width = 60
    height= 30
    im = Image.new('RGB',(width,height),bgcolor)
    draw = ImageDraw.Draw(im)
    for i in range(0,10):
        xy = (random.randrange(0,width),random.randrange(0,height))
        fillcolor = (random.randrange(0,255),255,random.randrange(0,255))
        draw.point(xy,fill = fillcolor)
    for i in range(0,5):
        xy = (random.randrange(0,width),random.randrange(0,height),random.randrange(0,width),random.randrange(0,height))
        fillcolor = (255,random.randrange(0,255),random.randrange(0,255))
        draw.line(xy,fill=fillcolor)
    str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
    rand_str = ''
    for i in range(0,4):
        rand_str+= str[random.randrange(0,len(str))]
    request.session['code'] = rand_str
    textcolor = (255,255,255)
    fontz = ImageFont.truetype('fonts-japanese-gothic.ttf',20)
    draw.text((5,random.randrange(0,15)),rand_str[0],fill=textcolor,font=fontz)
    draw.text((15, random.randrange(0, 15)), rand_str[1], fill=textcolor, font=fontz)
    draw.text((25, random.randrange(0, 15)), rand_str[2], fill=textcolor, font=fontz)
    draw.text((40, random.randrange(0, 15)), rand_str[3], fill=textcolor, font=fontz)
    buff = BytesIO()
    im.save(buff, 'png')
    return HttpResponse(buff.getvalue(), 'image/png')
def register(request):
    username = request.POST.get('username')
    password = request.POST.get('password')
    if password != cpwd:
        return redirect('/it_fj/register_handler1/')
    else:
        u = User()
        if u.findName(username):
            md = md5()
            md.update(password.encode())
            pwd = md.hexdigest()
            u.create(username, pwd, email)
        else:
            context = {'title': '注册', 'error': '用户名已存在'}
            return render(request, '/it_fj/register1.html/', context)
    return redirect('/login/')
    return render(request, '/it_fj/register.html/')

def register_handler(request):
    user = request.POST.get('user')
    password = request.POST.get('password')
    username = request.POST.get('username')
    cpwd = request.POST.get('cpwd')
    email = request.POST.get('e_mail')
    tel = request.POST.get('tel')
    addr = request.POST.get('addr')
    if password != cpwd:
        return redirect('/register/')
    else:
            u = User()
            if u.findName(username):
                md = md5()
                md.update(password.encode())
                pwd = md.hexdigest()
                u.create(user, pwd,username, email, tel, addr)
                return redirect('/login/')
            else:
                context = {'title':'注册','error':'用户名已存在'}
                return render(request,'/it_fj/login.html/',context)
def register1(request): #定义管理员注册页面
    username = request.POST.get('username')
    password = request.POST.get('password')
    pessions = request.POST.get('pessions') #取内部审核码的值
    print(pessions)
    a = user()
    a.pessions = pessions()
    if password != cpwd:
        return redirect('/it_fj/register_handler1/')
    else:
        u = User()
        if u.findName(username):
            md = md5()
            md.update(password.encode())
            pwd = md.hexdigest()
            u.create(username, pwd, email)
        else:
            context = {'title': '注册', 'error': '用户名已存在'}
            return render(request, '/it_fj/register1.html/', context)
    return redirect('/it_fj/login/')
def register_handler1(request):
    user = request.POST.get('user')
    password = request.POST.get('password')
    username = request.POST.get('username')
    cpwd = request.POST.get('cpwd')
    email = request.POST.get('e_mail')
    tel = request.POST.get('tel')
    addr = request.POST.get('addr')
    if password != cpwd:
        return redirect('/register/')
    else:
            u = User()
            if u.findName(username):
                md = md5()
                md.update(password.encode())
                pwd = md.hexdigest()
                u.create(user, pwd,username, email, tel, addr)
                return redirect('/login/')
            else:
                context = {'title':'注册','error':'用户名已存在'}
                return render(request,'/it_fj/login.html/',context)